﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Logging;
using System.Threading;

namespace SMTPTestSuite
{
    public partial class FormMain : Form
    {
        SmtpMock _smtpMock = null;
        Logger _logger = null;
        Encoding.FormBase64 _formBase64 = null;
        string _messageLogFolder = "";
        Thread _sendBulkFormThread = null;

        public FormMain()
        {
            InitializeComponent();
            _logger = new Logger(false, "");
            _logger.LogListBox = listBoxLog;
            if (!String.IsNullOrEmpty(textBoxReceivedMessagesFolder.Text))
            {
                _messageLogFolder = textBoxReceivedMessagesFolder.Text;
                if (!_messageLogFolder.EndsWith("\\"))
                    _messageLogFolder = _messageLogFolder + "\\";
            }
            
            _smtpMock = new SmtpMock(_logger,_messageLogFolder);
            UpdateOffline();
            _smtpMock.Start();
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            if (_sendBulkFormThread != null)
            {
                try
                {
                    _sendBulkFormThread.Abort();
                }
                catch { }
            }
            _smtpMock.Stop();
            _smtpMock = null;
            this.Dispose();
        }

        private void buttonSMTPSend_Click(object sender, EventArgs e)
        {
            // We start this form on a new thread, as this way it can also send a message copy to this SMTP
            // server (very useful for debugging)
            // If we don't do this on a new thread, then we'll end up locking when we attempt to send to ourselves

            if (_sendBulkFormThread != null)
            {
                if (_sendBulkFormThread.ThreadState == ThreadState.Running)
                    return;
                try
                {
                    _sendBulkFormThread.Abort();
                    _sendBulkFormThread = null;
                }
                catch { }
            }

            try
            {
                _sendBulkFormThread = new Thread(new ThreadStart(this.ShowSendBulkForm));
                _sendBulkFormThread.SetApartmentState(ApartmentState.STA);
                _sendBulkFormThread.Start();
                buttonSMTPSend.Enabled = false;
            }
            catch { }
        }

        private void ShowSendBulkForm()
        {
            FormSendBulk oForm = new FormSendBulk(listBoxLog);
            oForm.ShowDialog();
        }

        private void checkBoxRequireAuth_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                _smtpMock.OpenRelay = !checkBoxRequireAuth.Checked;
            }
            catch { }
        }

        private void buttonClearLog_Click(object sender, EventArgs e)
        {
            listBoxLog.Items.Clear();
        }

        private void buttonSendRaw_Click(object sender, EventArgs e)
        {
            FormSendRaw oForm = new FormSendRaw();
            oForm.Show(this);
        }

        private void buttonBase64_Click(object sender, EventArgs e)
        {
            if (_formBase64 == null)
            {
                _formBase64 = new Encoding.FormBase64();
                _formBase64.Show();
                return;
            }
            if (_formBase64.Visible) return;
            _formBase64.Show();
        }

        private void listBoxLog_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxLog.SelectedItems.Count != 1)
                return;
            try
            {
                textBoxLogEntry.Text = listBoxLog.SelectedItem.ToString();
                return;
            }
            catch { }
            textBoxLogEntry.Text = "";
        }

        private void buttonChooseLogFolder_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog oDialog = new OpenFileDialog();
            try
            {
                oDialog.FileName = _logger.LogFile;
            }
            catch
            {
            }
            oDialog.Filter = "Text files (*.txt)|*.txt|Log files (*.log)|*.log|All files (*.*)|*.* ";
            DialogResult result = oDialog.ShowDialog(this);
            if (result != System.Windows.Forms.DialogResult.OK)
                return;
            _logger.LogFile = oDialog.FileName;
        }

        private void buttonChooseMessageFolder_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog oDialog = new FolderBrowserDialog();
            try
            {
                oDialog.SelectedPath = System.IO.Path.GetFullPath(_messageLogFolder);
            }
            catch
            {
                oDialog.SelectedPath = Application.StartupPath;
            }
            oDialog.Description = "Select folder to store messages";
            DialogResult result = oDialog.ShowDialog(this);
            if (result != System.Windows.Forms.DialogResult.OK)
                return;
            _messageLogFolder = oDialog.SelectedPath;
            textBoxReceivedMessagesFolder.Text = _messageLogFolder;
        }

        private void textBoxReceivedMessagesFolder_TextChanged(object sender, EventArgs e)
        {
            _smtpMock.MessageStoreFolder = textBoxReceivedMessagesFolder.Text;
        }

        private void UpdateOffline()
        {
            if (checkBoxServerOffline.Checked)
            {
                _smtpMock.SetOffline(comboBoxOfflineReason.Text);
            }
            else
                _smtpMock.SetOnline();
        }

        private void checkBoxServerOffline_CheckedChanged(object sender, EventArgs e)
        {
            UpdateOffline();
        }

        private void comboBoxOfflineReason_TextUpdate(object sender, EventArgs e)
        {
             UpdateOffline();
        }

        private void comboBoxOfflineReason_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateOffline();
        }
    }
}
